package com.wzy.oa_sys.controller.task;


import com.wzy.oa_sys.model.request.task.TaskReq;
import com.wzy.oa_sys.model.response.task.TaskResp;
import com.wzy.oa_sys.service.task.TaskService;
import com.wzy.oa_sys.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/task")
public class TaskController {

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private TaskService taskService;

//    dept为0会导致查不到数据
    @GetMapping("/list")
    public List<TaskResp> getTaskList() {
        return taskService.getTaskList();
    }

    @PostMapping("/addTask")
    public void addTask(@RequestBody TaskReq taskReq, HttpServletRequest request){
        long l = jwtUtil.extractUserId(request);
        System.out.println("当前用户id："+l);
        System.out.println("新增任务："+taskReq.toString());
        taskService.addTask(taskReq,l);
    }

    @DeleteMapping("/deleteTaskById/{id}")
    public void deleteTaskById(@PathVariable("id") Integer id){
        System.out.println("删除任务："+id);
        int i = taskService.deleteTaskById(id);
        System.out.println("删除结果："+i);
    }

    @GetMapping("/getMyTaskList")
    public List<TaskResp> getMyTaskList(HttpServletRequest request){
        long l = jwtUtil.extractUserId(request);
        return taskService.getMyTaskList(l);
    }

    @GetMapping("/getTaskDetailById/{id}")
    public TaskResp getTaskDetailById(@PathVariable("id") Integer id){
        return taskService.getTaskDetailById(id);
    }

    @GetMapping("/getICanApprovedTask")
    public List<TaskResp> getICanApprovedTask(HttpServletRequest request){
        long l = jwtUtil.extractUserId(request);
        System.out.println("审核任务，当前用户id："+l);
        return taskService.getICanApprovedTask(l);
    }

    @PostMapping("/updateTheTask")
    public void updateTheTask(@RequestBody TaskReq taskReq, HttpServletRequest request){
        System.out.println("更新任务："+taskReq.toString());
        taskService.updateTheTask(taskReq);
    }

    @GetMapping("/getSomeTasks")
    public List<TaskResp> getSomeTasks(HttpServletRequest request){
        long l = jwtUtil.extractUserId(request);
        return taskService.getSomeTasks(l);
    }
    //编辑任务
    @PutMapping("/updateTask")
    public void updateTask(@RequestBody TaskReq taskReq,HttpServletRequest request){
        taskService.updateTask(taskReq,request);
    }

}
